package com.controller;

import com.mapper.AdminMapper;
import com.vo.adminVo.AdminLoginResponseVo;
import com.vo.adminVo.AdminRegisterResponseVo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Describe 登录注册控制器
 */
@Controller
public class LoginController {
    @Resource
    AdminMapper adminMapper;

    @RequestMapping({"/","/index.html"})
    public String index() {
        return "index";
    }


    /**
     *  注册电话信息验证
     * @param phone
     * @return
     */
    @RequestMapping("/register_verify")
    @ResponseBody
    public String registerVerify(String phone) {
        if (phone.equals("")){
            return "账号不能为空";
        }
//        判断手机号格式是否正确
//        else if(phone.matches(/^0?1[3|4|5|8]{0-9]\d{8}$/)){
//            return "请输入正确的手机号";
//        }
        List<AdminLoginResponseVo> adminLoginResponseVoList = adminMapper.getAdminLoginInfo();
        for(int i=0;i<adminLoginResponseVoList.size();i++) {
            AdminLoginResponseVo admin = adminLoginResponseVoList.get(i);
            if (admin.getPhone().equals(phone)) {
                return "账号已存在请重新输入";
            }
        }
        return "账号可用";
    }

//    public boolean checkPhone(String phone){
//        Pattern pattern = Pattern.compile("^13\d{9}||15[8,9]\d{8}$");
//        Matcher matcher = pattern.matcher(phone);
//
//        if (matcher.matches()) {
//            return true;
//        }
//        return false;
//    }
//

//    public static boolean isMobile(String str) {
//        Pattern p = null;
//        Matcher m = null;
//        boolean b = false;
//        String s2 = "^[1](([3][0-9])|([4][5,7,9])|([5][^4,6,9])|([6][6])|([7][3,5,6,7,8])|([8][0-9])([9][8,9]))[0-9]{
//        if (StringUtils.isNotBlank(str)) {
//            p = Pattern.compile(s2);
//            m = p.matcher(str);
//            b = m.matches();
//        }
//        return b;
//    }





    /**
     * 注册管理员账号
     * @param
     * @return
     */
    @RequestMapping("/register")
    @ResponseBody
    public String register(String phone,String password,String checkPassword) {
        List<AdminLoginResponseVo> adminLoginResponseVoList = adminMapper.getAdminLoginInfo();
        if(!password.equals(checkPassword)) {
            return "请重新确认密码";
        }
        else if(phone.equals("")) {
            return "账号不能为空";
        }

        for(int i=0;i<adminLoginResponseVoList.size();i++) {
                AdminLoginResponseVo admin=adminLoginResponseVoList.get(i);
                if (admin.getPhone().equals(phone)) {
                    return "此账号已注册,请重新输入或选择登录";
                }
            }

        AdminRegisterResponseVo vo = new AdminRegisterResponseVo();
        vo.setPassword(password);
        vo.setPhone(phone);
        vo.setCreateTime(LocalDateTime.now());
        adminMapper.registerAdmin(vo);
        return "注册成功";
    }



    /**
     * 登录时电话信息验证
     * @param phone
     * @return
     */
    @RequestMapping("/verify")
    @ResponseBody
    public String verify(String phone) {
        List<AdminLoginResponseVo> adminLoginResponseVoList = adminMapper.getAdminLoginInfo();
        for(int i=0;i<adminLoginResponseVoList.size();i++)
        {
            AdminLoginResponseVo admin=adminLoginResponseVoList.get(i);
            if(admin.getPhone().equals(phone)) {
                return "账号正确";
            }
            else if(admin.getPhone().equals("")){
                return "请输入账号";
            }
            else if(!admin.getPhone().equals(phone) && !admin.getPhone().equals("") && adminLoginResponseVoList.size()==i+1) {
                return "账号未注册";
            }
        }
        return "manager";
    }


    /**
     * 登录时密码验证
     * @param phone
     * @param password
     * @return
     */
    @RequestMapping("/password_verify")
    @ResponseBody
    public String passwordVerify(String phone,String password){
        List<AdminLoginResponseVo> adminLoginResponseVoList = adminMapper.getAdminLoginInfo();
        for(int i=0;i<adminLoginResponseVoList.size();i++) {
            AdminLoginResponseVo admin=adminLoginResponseVoList.get(i);
            if(admin.getPhone().equals(phone)) {
                if (admin.getPassword().equals(password)) {
                    return "密码正确";
                }
                else if (!admin.getPassword().equals(password)) {
                    return "密码错误请重试";
                }
            }
            else if(!admin.getPhone().equals(phone) && adminLoginResponseVoList.size()==i+1){
                return "账号错误请重试";
            }
        }
        return "验证失败";
    }

    /**
     * 管理员账号登录信息验证+权限验证
     * @param phone
     * @param password
     * @param session
     * @param model
     * @return
     */
    @RequestMapping("/login")
    public ModelAndView  login(String phone, String password, HttpSession session, Model model){
        ModelAndView mav = new ModelAndView();
        List<AdminLoginResponseVo> adminLoginResponseVoList = adminMapper.getAdminLoginInfo();
        for(int i = 0 ; i <adminLoginResponseVoList.size() ; i++) {
            AdminLoginResponseVo admin = new AdminLoginResponseVo();
            admin = adminLoginResponseVoList.get(i);
            if(admin.getPhone().equals(phone)) {
                if(admin.getPassword().equals(password)) {
                    session.setAttribute("user",phone);
                    model.addAttribute("s",phone);
                    if(admin.getLimit() == 1) {
                        mav.setViewName("manager");
                        return mav;
                    }
                    else if(admin.getLimit()==0) {
                        mav.setViewName("user");
                        return mav;
                    }
                }
                else if(!admin.getPassword().equals(password)) {
                    mav.addObject("shen","用户名错误");
                }
            }
            else if(adminLoginResponseVoList.size() == i+1 && !admin.getPassword().equals(password)) {
                mav.addObject("shen","密码错误");
            }
        }
        return mav;
    }

    /**
     * 重新加载管理员登录界面
     * @return
     */
    @RequestMapping("/reLogin")
    public String reLogin() {
        return "managerLogin";
    }

    /**
     * 账号登出
     * @param session
     * @return
     */
    @RequestMapping("/logout")
    @ResponseBody
    public String  logout(HttpSession session) {
        session.setAttribute("user",null);
        return "注销成功";
    }
}
